package com.itheima.controller.home;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.api.Sms.HomeBrandService;
import com.itheima.vo.domain.AjaxResult;
import com.itheima.vo.domain.TableDataInfo;
import com.itheima.vo.pojo.SmsHomeAdvertise;
import com.itheima.vo.pojo.SmsHomeBrand;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("home/brand")
public class SmsHomeBrandController {

    @DubboReference
    HomeBrandService brandService;

    /*
    * 1.添加首页推荐品牌
    * */
    @PostMapping("/create")
    public AjaxResult addIndex(@RequestBody List<SmsHomeBrand> smsHomeBrand)
    {
        boolean save = brandService.saveBatch(smsHomeBrand);

        return AjaxResult.success(save);
    }

    /*
    * 2.批量删除推荐品牌
    * */

    @PostMapping("/delete")
    public AjaxResult delBatch(@RequestParam List<Integer>  ids)
    {

        boolean b = brandService.removeByIds(ids);

        return AjaxResult.success(b);
    }


    /*
    *  3.分页查询推荐广告
    * */
    @GetMapping("/list")
    public AjaxResult AllPage(Integer pageNum , Integer pageSize , String brandName , Integer recommendStatus)
    {
        LambdaQueryWrapper<SmsHomeBrand> lambdaQueryWrapper  =new LambdaQueryWrapper<>();
        lambdaQueryWrapper.ge(brandName !=null,SmsHomeBrand::getBrandName, brandName);
        lambdaQueryWrapper.like(recommendStatus !=null ,SmsHomeBrand::getRecommendStatus, recommendStatus);
        TableDataInfo tableDataInfo  = new TableDataInfo();
        Page<SmsHomeBrand> page = new Page<>(pageNum , pageSize);
        Page<SmsHomeBrand> page1 = brandService.page(page , lambdaQueryWrapper);

        tableDataInfo.setList(page1.getRecords());

        tableDataInfo.setPageNum(pageNum);

        tableDataInfo.setPageSize(pageSize);

        tableDataInfo.setTotal(page1.getTotal());
        tableDataInfo.setTotalPage(page1.getPages());

        return AjaxResult.success(tableDataInfo);
    }


//    /*
//    *
//    * 4.批量修改状态
//    * */
//    @PostMapping("update/recommendStatus")
//    public AjaxResult StatusAlterBatch (@RequestBody ArrayList<SmsBatch> smsHomeBrand)
//    {
//        SmsHomeBrand smsHomeBrand1 = new SmsHomeBrand();
//        ArrayList<SmsHomeBrand> brands = new ArrayList<>();
//
//        for (SmsBatch smsBatch : smsHomeBrand) {
//
//            smsHomeBrand1.setBrandId(smsBatch.getIds());
//            smsHomeBrand1.setRecommendStatus(smsBatch.getRecommendStatus());
//            brands.add(smsHomeBrand1);
//        }
//
//
//        boolean b1 = brandService.updateBatchById(brands);
//
//        return AjaxResult.success(b1);
//    }

    /*
    * 4.修改推荐品牌状态
    * */
    @PostMapping("update/recommendStatus")
    public AjaxResult BatchAlterStatus(@RequestParam ArrayList<Integer> ids ,Integer recommendStatus)
    {


        ArrayList<SmsHomeBrand> brands = new ArrayList<>();

        for (Integer id : ids) {
            SmsHomeBrand smsHomeBrand = new SmsHomeBrand();
            smsHomeBrand.setId(id);
            smsHomeBrand.setRecommendStatus(recommendStatus);
            brands.add(smsHomeBrand);
        }

        boolean b = brandService.updateBatchById(brands);

        return AjaxResult.success(b);
    }

    /*
    * 5.修改品牌排序
    * */
    @PostMapping("update/sort/{id}")
    public AjaxResult AlterSort(@PathVariable Integer id ,Integer sort)
    {

        SmsHomeBrand brand = new SmsHomeBrand();
        brand.setBrandId(id);
        brand.setSort(sort);
        Boolean b =  brandService.updateById(brand);

        return AjaxResult.success(b)
;    }

}
